package com.innovation.chengzhangriji.ui;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.orhanobut.logger.Logger;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HeadGalleryDialogActivity extends BaseActivity {
    private Bitmap image;
    private AlertDialog alertDialog;
    private DisplayImageOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String url = getIntent().getStringExtra("url");
        setContentView(R.layout.activity_head_gallery_dialog);
        final ImageView imageView = (ImageView) findViewById(R.id.image);
        imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                alertDialog = new AlertDialog.Builder(HeadGalleryDialogActivity.this).create();
                alertDialog.show();
                alertDialog.getWindow().setGravity(Gravity.BOTTOM);
                alertDialog.getWindow().setContentView(R.layout.dialog_savephoto);
                alertDialog.getWindow()
                        .findViewById(R.id.tv_phote_cancle)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                alertDialog.dismiss();
                            }
                        });
                alertDialog.getWindow()
                        .findViewById(R.id.tv_phote_save)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                image = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                                Log.d("2asd", "" + image);
                                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                String date = df.format(new Date());
                                date = date.substring(0, 4) + date.substring(5, 7) + date.substring(8, 10) + "_" + date.substring(11, 13) + date.substring(14, 16) + date.substring(17, 19);
                                String nowdate = Environment.getExternalStorageDirectory() + "/DCIM/Camera/" + date + ".jpg";
                                try {
                                    File myCaptureFile = new File(nowdate);
                                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
                                    image.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                                    bos.flush();
                                    bos.close();
                                } catch (Exception e) {

                                }
                                alertDialog.dismiss();
                                tipShow("已保存到系统相册");
                            }
                        });
                return false;
            }
        });
        setOptions();
        ImageLoader.getInstance().displayImage(url, imageView, options);
    }

    private void setOptions() {
        options = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)   //加载图片时会在磁盘中加载缓存
                .build();
    }
}
